package cn.coderdream.mtoast;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;

public class ToastView extends RelativeLayout {
    private Context mContext;
    private String content;
    private int content_color = Color.parseColor("#ffffff");
    private int backgroundSrc = R.drawable.toast_default_shape;
    private int icon = R.mipmap.toast_success;
    private ImageView mToast_icon;
    private ImageView mToast_background;
    private TextView mToast_message;

    public ToastView(Context context) {
        this(context, null);
    }

    public ToastView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ToastView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.ToastView, defStyleAttr, 0);

        View view = LayoutInflater.from(mContext).inflate(R.layout.toast_view, this);
        mToast_icon = view.findViewById(R.id.mToast_icon);
        mToast_message = view.findViewById(R.id.mToast_message);
        mToast_background = view.findViewById(R.id.mToast_background);
        try {
            content = typedArray.getString(R.styleable.ToastView_mToastView_content);
            content_color = typedArray.getColor(R.styleable.ToastView_mToastView_content_color, Color.parseColor("#ffffff"));
            backgroundSrc = typedArray.getResourceId(R.styleable.ToastView_mToastView_backgroundResources, 0);
            icon = typedArray.getResourceId(R.styleable.ToastView_mToastView_icon, 0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mToast_message.setText(content);
            mToast_message.setTextColor(content_color);
            if (backgroundSrc != 0) {
                mToast_background.setImageResource(backgroundSrc);
            }

            if (icon != 0) {
                mToast_icon.setImageResource(icon);
                mToast_icon.setVisibility(VISIBLE);
            } else {
                mToast_icon.setVisibility(GONE);
            }

            typedArray.recycle();
        }
    }

    public void setContent(String content) {
        this.content = content;
        if (mToast_message != null) {
            mToast_message.setText(content);
        }
    }

    public void setContent_color(@ColorInt int color) {
        this.content_color = color;
        if (mToast_message != null) {
            mToast_message.setTextColor(this.content_color);
        }
    }

    public void setBackgroundResources(int backgroundSrc) {
        this.backgroundSrc = backgroundSrc;
        if (mToast_background != null) {
            if (backgroundSrc != 0) {
                mToast_background.setImageResource(backgroundSrc);
            }
        }
    }

    public void setIcon(int icon) {
        this.icon = icon;
        if (mToast_icon != null) {
            if (icon != 0) {
                mToast_icon.setVisibility(VISIBLE);
                mToast_icon.setImageResource(icon);
            } else {
                mToast_icon.setVisibility(GONE);
            }
        }
    }

}
